
import requests
from lxml import etree
from flask import Flask, render_template,request

from 实战.抽奖.app import app


def get_mobile(phone):
    # 发送请求的地址
    url = f'https://ip138.com/mobile.asp?mobile={phone}&action=mobile'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 SLBrowser/9.0.3.1311 SLBChan/105'
    }
    # 发送请求
    resp = requests.get(url, headers=headers)
    # 设置中文显示
    resp.encoding = 'utf-8'
    # 解析数据
    e = etree.HTML(resp.text)
    # 编写xpath提取数据
    province = e.xpath('//tr/td[2]/span/text()')
    city = e.xpath('//tr/td[2]/span/a/text()')
    if(len(province) > 0 and len(city) > 0):
        datas = province[0] + city[0]
        print(datas)
        return datas
    else:
        print("手机号码不正确！")
        return "手机号码不正确！"

# get_mobile(input('请输入手机号码：'))

app = Flask(__name__)
@app.route('/')
def index():
    return render_template('index.html')
@app.route('/search_phone')
def search_phone():
    phone = request.args.get('phone')
    data = get_mobile(phone)
    return data

app.run(debug=True)